package org.sam.swing.table.matrixImpl;

import java.util.List;
import java.util.Map;

import org.sam.swing.table.JSTable;
import org.sam.swing.table.JSTableColumn;
import org.sam.swing.table.JSTableModel;
import org.sam.swing.table.JSTableModelEvent;
import org.sam.swing.table.JSTableModelLinster;

/**
 * 矩阵数据类型的具体实现类对象
 * 
 * @author sam
 *
 * @param <E>
 */
public class JSTableModelMatrixAdapter<G, T> implements JSTableModelLinster<Map<G, List<T>>> {

	/**
	 * 当前的表格对象
	 */
	private JSTable table;

	/**
	 * 当前的列对象
	 */
	private JSTableMatrixColumnModel colModel;

	/**
	 * 获取表格对象
	 * 
	 * @return
	 */
	public JSTable getTable() {
		return table;
	}

	/**
	 * 设置表格对象，包内可见
	 * 
	 * @param table
	 */
	void setTable(JSTable table) {
		this.table = table;
	}

	/**
	 * 当前的列对象
	 * 
	 * @return
	 */
	public JSTableMatrixColumnModel getColModel() {
		return colModel;
	}

	/**
	 * 当前的列对象 包内可见
	 * 
	 * @param colModel
	 */
	void setColModel(JSTableMatrixColumnModel colModel) {
		this.colModel = colModel;
	}
	
	/**
	 * 是否初始化完成
	 */
	private volatile boolean isRetrieved = false;

	/**
	 * 矩阵数据类型的具体实现类对象
	 */
	public JSTableModelMatrixAdapter() {
		super();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void beforeRetrieve(JSTableModelEvent event) throws Exception {
		event.getTableModel().clear();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Map<G, List<T>> onRetrieve() throws Exception {
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void afterRetrieve(JSTableModelEvent event) throws Exception {
		
		if (!isRetrieved) {
			
			JSTableMatrixModel<G, List<T>> model = (JSTableMatrixModel<G, List<T>>) event
					.getTableModel();
			
			JSTableMatrixVColumn vColumn = model.getVColumn();
			JSTableMatrixHColumn hColumn = model.getHColumn();
			JSTableMatrixValueColumn valueColumn = model.getValueColumn();
			
			colModel.removeColumn(hColumn);
			colModel.removeColumn(vColumn);
			colModel.removeColumn(valueColumn);
			
			JSTableColumn[] matrixColumns = model.getMatrixColumns();
			for (int i = 0; i < matrixColumns.length; i++) {
				colModel.addColumn(matrixColumns[i]);
			}
			table.setColumnModel(colModel);
			isRetrieved = true;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void beforeUpdate(JSTableModelEvent event) throws Exception {

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean update(JSTableModelEvent event) throws Exception {
		return true;
	}

	@Override
	public void afterUpdate(JSTableModelEvent event) throws Exception {
		event.getTableModel().resetUpdate();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void beforeDelete(JSTableModelEvent event) throws Exception {

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void afterDelete(JSTableModelEvent event) throws Exception {

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void beforeAppend(JSTableModelEvent event) throws Exception {

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void afterAppend(JSTableModelEvent event) throws Exception {

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void beforeInsert(JSTableModelEvent event) throws Exception {

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void afterInsert(JSTableModelEvent event) throws Exception {

	}

	/**
	 * {@inheritDoc}
	 */
	public boolean check(JSTableModel<?> tableModel , Map<Integer, String> result){
		return true;
	}
}
